package com.megagoodsoftware.MediaShare.fakes;

import java.io.*;
import java.util.*;

import org.apache.http.*;
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.conn.*;
import org.apache.http.entity.*;
import org.apache.http.message.*;
import org.apache.http.params.*;
import org.apache.http.protocol.*;

import com.megagoodsoftware.smugmug.*;

public class FakeHttpClient implements HttpClient {
	
	private HashMap<String, String> responseMap;
	private HashMap<String, String> successResponseMap;
	private HashMap<String, String> failureResponseMap;
	
	private static final String LOGIN_HASH_RESPONSE = 
		"{\"stat\":\"ok\",\"method\":\"" + SmugMugClient.METHOD_LOGIN_WITH_HASH + "\",\"Login\":{\"User\":{\"NickName\":\"fakenick\",\"DisplayName\":\"Fake Name\"},\"AccountType\":\"Power\",\"FileSizeLimit\":12582912,\"SmugVault\":false,\"Session\":{\"id\":\"abcdef\"}}}";
	private static final String LOGIN_FAILED_RESPONSE =
		"{\"stat\":\"fail\",\"method\":\"" + SmugMugClient.METHOD_LOGIN_WITH_HASH + "\",\"code\":1,\"message\":\"invalid login\"}";
	private static final String LOGIN_EMAIL_RESPONSE =
		"{\"stat\": \"ok\",\"method\": \"" + SmugMugClient.METHOD_LOGIN_WITH_PASSWORD + "\",\"Login\": {\"AccountStatus\": \"Active\",\"AccountType\": \"Pro\",\"FileSizeLimit\": 25165824,\"PasswordHash\": \"$1$qZ2KzsuJ$GllBdngm3V5KLaY7Hkhcu/\",\"Session\": {\"id\": \"d2964l9adecc9a6843bfaa4alba5fa22\"},\"SmugVault\": true,\"User\": {\"id\": 1234,\"DisplayName\": \"Fred Nerk\",\"NickName\": \"fred\",\"URL\": \"http://fred.smugmug.com\"}}}";
	private static final String ALBUMS_RESPONSE =
		"{\"stat\":\"ok\",\"method\":\"" + SmugMugClient.METHOD_ALBUMS_GET + "\",\"Albums\":[{\"id\":2398748,\"Key\":\"dVPoA\",\"Category\":{\"id\":0,\"Name\":\"Other\"},\"Title\":\"Album1\"},{\"id\":2398749,\"Key\":\"eVPoA\",\"Category\":{\"id\":1,\"Name\":\"Animals\"},\"Title\":\"Album2\"},{\"id\":2398750,\"Key\":\"fVPoA\",\"Category\":{\"id\":2,\"Name\":\"Architecture\"},\"Title\":\"Album3\"},{\"id\":2398751,\"Key\":\"gVPoA\",\"Category\":{\"id\":3,\"Name\":\"Art\"},\"Title\":\"Album 4\"},{\"id\":2398752,\"Key\":\"hVPoA\",\"Category\":{\"id\":3,\"Name\":\"Art\"},\"Title\":\"Album 5\"},{\"id\":2398753,\"Key\":\"iVPoA\",\"Category\":{\"id\":4,\"Name\":\"Boats\"},\"Title\":\"Album 6\"}]}";
	private static final String UPLOAD_RESPONSE = 
		"{\"stat\":\"ok\",\"method\":\"" + SmugMugClient.METHOD_IMAGES_UPLOAD + "\",\"Image\":{\"id\":201510850,\"Key\":\"bQ8fG\",\"URL\":\"http://trinhphotos.smugmug.com/Other/Misc/5157813_bKhKu#803133140_bQ8fG\"}}";
	private static final String IMAGES_GET_RESPONSE =
		"{\"method\":\"smugmug.images.get\",\"stat\":\"ok\",\"Images\":[{\"Caption\":\"Random Caption\",\"Format\":\"JPG\",\"TinyURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2114/365044642_sbdNt-Ti.jpg\",\"Key\":\"sbdNt\",\"Type\":\"Album\",\"id\":365044642,\"Date\":\"2008-09-03 13:30:53\",\"Width\":3264,\"Keywords\":\"\",\"Album\":{\"URL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/5876162_TtgFD#365044642_sbdNt\",\"id\":5876162,\"Key\":\"TtgFD\"},\"Size\":4213181,\"MediumURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2114/365044642_sbdNt-M.jpg\",\"LastUpdated\":\"2008-09-03 13:31:32\",\"XLargeURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2114/365044642_sbdNt-XL.jpg\",\"MD5Sum\":\"6aac158f46e73546e0c6d0df56b53ee5\",\"Hidden\":false,\"Height\":2448,\"Watermark\":false,\"X2LargeURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2114/365044642_sbdNt-X2.jpg\",\"OriginalURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2114/365044642_sbdNt-O.jpg\",\"Serial\":0,\"X3LargeURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2114/365044642_sbdNt-X3.jpg\",\"AlbumURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/5876162_TtgFD#365044642_sbdNt\",\"FileName\":\"IMG_2114.jpg\",\"LargeURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2114/365044642_sbdNt-L.jpg\",\"Position\":1,\"SmallURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2114/365044642_sbdNt-S.jpg\",\"ThumbURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2114/365044642_sbdNt-Th.jpg\"},{\"Caption\":\"\",\"Format\":\"JPG\",\"TinyURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2115/365044839_epjzJ-Ti.jpg\",\"Key\":\"epjzJ\",\"Type\":\"Album\",\"id\":365044839,\"Date\":\"2008-09-03 13:31:14\",\"Width\":3264,\"Keywords\":\"\",\"Album\":{\"URL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/5876162_TtgFD#365044839_epjzJ\",\"id\":5876162,\"Key\":\"TtgFD\"},\"Size\":3963434,\"MediumURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2115/365044839_epjzJ-M.jpg\",\"LastUpdated\":\"2008-09-03 13:31:58\",\"XLargeURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2115/365044839_epjzJ-XL.jpg\",\"MD5Sum\":\"893797be588c9053fbb311ca9637b4ef\",\"Hidden\":false,\"Height\":2448,\"Watermark\":false,\"X2LargeURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2115/365044839_epjzJ-X2.jpg\",\"OriginalURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2115/365044839_epjzJ-O.jpg\",\"Serial\":0,\"X3LargeURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2115/365044839_epjzJ-X3.jpg\",\"AlbumURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/5876162_TtgFD#365044839_epjzJ\",\"FileName\":\"IMG_2115.jpg\",\"LargeURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2115/365044839_epjzJ-L.jpg\",\"Position\":2,\"SmallURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2115/365044839_epjzJ-S.jpg\",\"ThumbURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2115/365044839_epjzJ-Th.jpg\"},{\"Caption\":\"\",\"Format\":\"JPG\",\"TinyURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2116/365045043_4yiuH-Ti.jpg\",\"Key\":\"4yiuH\",\"Type\":\"Album\",\"id\":365045043,\"Date\":\"2008-09-03 13:31:36\",\"Width\":3264,\"Keywords\":\"\",\"Album\":{\"URL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/5876162_TtgFD#365045043_4yiuH\",\"id\":5876162,\"Key\":\"TtgFD\"},\"Size\":4321552,\"MediumURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2116/365045043_4yiuH-M.jpg\",\"LastUpdated\":\"2008-09-03 13:32:18\",\"XLargeURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2116/365045043_4yiuH-XL.jpg\",\"MD5Sum\":\"d5af3e02afae08047d57f5060b0ed5da\",\"Hidden\":false,\"Height\":2448,\"Watermark\":false,\"X2LargeURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2116/365045043_4yiuH-X2.jpg\",\"OriginalURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2116/365045043_4yiuH-O.jpg\",\"Serial\":0,\"X3LargeURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2116/365045043_4yiuH-X3.jpg\",\"AlbumURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/5876162_TtgFD#365045043_4yiuH\",\"FileName\":\"IMG_2116.jpg\",\"LargeURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2116/365045043_4yiuH-L.jpg\",\"Position\":3,\"SmallURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2116/365045043_4yiuH-S.jpg\",\"ThumbURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2116/365045043_4yiuH-Th.jpg\"},{\"Caption\":\"\",\"Format\":\"JPG\",\"TinyURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2117/365045228_ufh5q-Ti.jpg\",\"Key\":\"ufh5q\",\"Type\":\"Album\",\"id\":365045228,\"Date\":\"2008-09-03 13:31:57\",\"Width\":3264,\"Keywords\":\"\",\"Album\":{\"URL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/5876162_TtgFD#365045228_ufh5q\",\"id\":5876162,\"Key\":\"TtgFD\"},\"Size\":3625533,\"MediumURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2117/365045228_ufh5q-M.jpg\",\"LastUpdated\":\"2008-09-03 13:32:42\",\"XLargeURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2117/365045228_ufh5q-XL.jpg\",\"MD5Sum\":\"a6de76b401b37140beaf83a8b8603c07\",\"Hidden\":false,\"Height\":2448,\"Watermark\":false,\"X2LargeURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2117/365045228_ufh5q-X2.jpg\",\"OriginalURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2117/365045228_ufh5q-O.jpg\",\"Serial\":0,\"X3LargeURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2117/365045228_ufh5q-X3.jpg\",\"AlbumURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/5876162_TtgFD#365045228_ufh5q\",\"FileName\":\"IMG_2117.jpg\",\"LargeURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2117/365045228_ufh5q-L.jpg\",\"Position\":4,\"SmallURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2117/365045228_ufh5q-S.jpg\",\"ThumbURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2117/365045228_ufh5q-Th.jpg\"},{\"Caption\":\"\",\"Format\":\"JPG\",\"TinyURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2118/365045388_2W6tc-Ti.jpg\",\"Key\":\"2W6tc\",\"Type\":\"Album\",\"id\":365045388,\"Date\":\"2008-09-03 13:32:17\",\"Width\":3264,\"Keywords\":\"\",\"Album\":{\"URL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/5876162_TtgFD#365045388_2W6tc\",\"id\":5876162,\"Key\":\"TtgFD\"},\"Size\":3979909,\"MediumURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2118/365045388_2W6tc-M.jpg\",\"LastUpdated\":\"2008-09-03 13:33:23\",\"XLargeURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2118/365045388_2W6tc-XL.jpg\",\"MD5Sum\":\"066e006c5cf2e97098ddc9d4fa530f90\",\"Hidden\":false,\"Height\":2448,\"Watermark\":false,\"X2LargeURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2118/365045388_2W6tc-X2.jpg\",\"OriginalURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2118/365045388_2W6tc-O.jpg\",\"Serial\":0,\"X3LargeURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2118/365045388_2W6tc-X3.jpg\",\"AlbumURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/5876162_TtgFD#365045388_2W6tc\",\"FileName\":\"IMG_2118.jpg\",\"LargeURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2118/365045388_2W6tc-L.jpg\",\"Position\":5,\"SmallURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2118/365045388_2W6tc-S.jpg\",\"ThumbURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2118/365045388_2W6tc-Th.jpg\"},{\"Caption\":\"\",\"Format\":\"JPG\",\"TinyURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2119/365045493_uDyuc-Ti.jpg\",\"Key\":\"uDyuc\",\"Type\":\"Album\",\"id\":365045493,\"Date\":\"2008-09-03 13:32:31\",\"Width\":3264,\"Keywords\":\"\",\"Album\":{\"URL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/5876162_TtgFD#365045493_uDyuc\",\"id\":5876162,\"Key\":\"TtgFD\"},\"Size\":3525344,\"MediumURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2119/365045493_uDyuc-M.jpg\",\"LastUpdated\":\"2008-09-03 13:33:22\",\"XLargeURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2119/365045493_uDyuc-XL.jpg\",\"MD5Sum\":\"38ba5061be6e7e2f4bd6c30f33f4bda6\",\"Hidden\":false,\"Height\":2448,\"Watermark\":false,\"X2LargeURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2119/365045493_uDyuc-X2.jpg\",\"OriginalURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2119/365045493_uDyuc-O.jpg\",\"Serial\":0,\"X3LargeURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2119/365045493_uDyuc-X3.jpg\",\"AlbumURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/5876162_TtgFD#365045493_uDyuc\",\"FileName\":\"IMG_2119.jpg\",\"LargeURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2119/365045493_uDyuc-L.jpg\",\"Position\":6,\"SmallURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2119/365045493_uDyuc-S.jpg\",\"ThumbURL\":\"http://trinhphotos.smugmug.com/Other/XC1-Construction/IMG2119/365045493_uDyuc-Th.jpg\"}]}";
	private static final String LOGOUT_RESPONSE = 
		"{\"stat\":\"ok\",\"method\":\"" + SmugMugClient.METHOD_LOGOUT + "\",\"Logout\":{\"Successful\":\"\"}}";
	private static final String INVALID_SESSION_RESPONSE = 
		"{\"stat\":\"fail\", \"code\":3,\"message\":\"invalid session\"}";
	private static final String CATEGORIES_RESPONSE = 
		"{\"stat\": \"ok\",\"method\": \"" + SmugMugClient.METHOD_CATEGORIES_GET + "\",\"Categories\": [{\"id\": 0,\"Name\": \"Other\",\"NiceName\": \"Other\",\"Type\": \"SmugMug\"},{\"id\": 1,\"Name\": \"Animals\",\"NiceName\": \"Animals\",\"Type\": \"SmugMug\"},{\"id\": 2,\"Name\": \"Architecture\",\"NiceName\": \"Architecture\",\"Type\": \"SmugMug\"},{\"id\": 3,\"Name\": \"Art\",\"NiceName\": \"Art\",\"Type\": \"SmugMug\"},{\"id\": 4,\"Name\": \"Boats\",\"NiceName\": \"Boats\",\"Type\": \"SmugMug\"},{\"id\": 5,\"Name\": \"Business\",\"NiceName\": \"Business\",\"Type\": \"SmugMug\"}]}";
	
	private boolean invalidSession;

	public void setInvalidSession(boolean invalidSession) {
		this.invalidSession = invalidSession;
	}
	public FakeHttpClient()
	{
		successResponseMap = new HashMap<String, String>();
		successResponseMap.put(SmugMugClient.METHOD_LOGIN_WITH_HASH, LOGIN_HASH_RESPONSE);
		successResponseMap.put(SmugMugClient.METHOD_LOGIN_WITH_PASSWORD, LOGIN_EMAIL_RESPONSE);
		successResponseMap.put(SmugMugClient.METHOD_ALBUMS_GET, ALBUMS_RESPONSE);
		successResponseMap.put(SmugMugClient.METHOD_IMAGES_UPLOAD, UPLOAD_RESPONSE);
		successResponseMap.put(SmugMugClient.METHOD_LOGOUT, LOGOUT_RESPONSE);
		successResponseMap.put(SmugMugClient.METHOD_CATEGORIES_GET, CATEGORIES_RESPONSE);
		successResponseMap.put(SmugMugClient.METHOD_IMAGES_GET, IMAGES_GET_RESPONSE);
		
		failureResponseMap = new HashMap<String, String>();
		failureResponseMap.put(SmugMugClient.METHOD_LOGIN_WITH_HASH, LOGIN_FAILED_RESPONSE);
		
		// defaulting responses to success case
		responseMap = successResponseMap;
		invalidSession = false;
	}

	public void setSuccessResponseMap() {
		this.responseMap = successResponseMap;
	}
	public void setFailureResponseMap() {
		this.responseMap = failureResponseMap;
	}

	@Override
	public HttpResponse execute(HttpUriRequest request) throws IOException,
			ClientProtocolException {
		HttpResponse resp = new BasicHttpResponse(new ProtocolVersion("HTTP", 3, 0),200, "ok");
		HttpEntity entity;
		if (invalidSession)
			entity = new StringEntity(INVALID_SESSION_RESPONSE);
		else
		{
			String response;
			if (request.getURI().getHost().contains("upload.smugmug.com"))
				response = responseMap.get(SmugMugClient.METHOD_IMAGES_UPLOAD);
			else {
				String s = request.getURI().toASCIIString();
				response = responseMap.get(s.substring(s.indexOf('=')+1, s.indexOf('&')));
			}
			entity = new StringEntity(response);
		}
			
		resp.setEntity(entity);
		return resp;
	}

	@Override
	public HttpResponse execute(HttpUriRequest request, HttpContext context)
			throws IOException, ClientProtocolException {
		return null;
	}

	@Override
	public HttpResponse execute(HttpHost target, HttpRequest request)
			throws IOException, ClientProtocolException {
		return null;
	}

	@Override
	public <T> T execute(HttpUriRequest arg0, ResponseHandler<? extends T> arg1)
			throws IOException, ClientProtocolException {
		return null;
	}

	@Override
	public HttpResponse execute(HttpHost target, HttpRequest request,
			HttpContext context) throws IOException, ClientProtocolException {
		return null;
	}

	@Override
	public <T> T execute(HttpUriRequest arg0,
			ResponseHandler<? extends T> arg1, HttpContext arg2)
			throws IOException, ClientProtocolException {
		return null;
	}

	@Override
	public <T> T execute(HttpHost arg0, HttpRequest arg1,
			ResponseHandler<? extends T> arg2) throws IOException,
			ClientProtocolException {
		return null;
	}

	@Override
	public <T> T execute(HttpHost arg0, HttpRequest arg1,
			ResponseHandler<? extends T> arg2, HttpContext arg3)
			throws IOException, ClientProtocolException {
		return null;
	}

	@Override
	public ClientConnectionManager getConnectionManager() {
		return null;
	}

	@Override
	public HttpParams getParams() {
		return null;
	}

}
